package com.casic.worklog.controller;

import com.casic.worklog.model.EmpSign;
import com.casic.worklog.model.vo.Result;

import com.casic.worklog.service.SignService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Administrator
 * @date 2019/9/16 19:56
 */
@Controller
@RequestMapping("/sign")
public class SignController {

    @Resource
    private SignService signService;

    @RequestMapping("/signIn")
    @ResponseBody
    public Result signIn(EmpSign empSign,Short id){
        //将当前时间放入对象
        empSign.setStartTime(new Date());
        EmpSign OldEmpSign = signService.selectStatTime(id);
        if(OldEmpSign !=null ){
            return Result.ok("你已经签过了");
        }else {
            empSign.setId(id);
            signService.insert(empSign);
            return Result.ok("签到成功");
        }


    }

    @RequestMapping("/signOut")
    @ResponseBody
    public Result signOut(EmpSign empSign,Short id){
        empSign.setEndTime(new Date());
        //查询今日已签退时间
        Date signOutTime = signService.selectEndTime(id);
        EmpSign oldEmpSign = signService.selectStatTime(id);
        //今日未签到则无数据
        if(oldEmpSign == null){
            return Result.ok("今天还没有签到哦");
        }else{
            empSign.setSignId(oldEmpSign.getSignId());
            if(signOutTime != null){
                //根据日志id更新签退时间
                signService.updateBySignId(empSign);
                return Result.ok("晚上好！签退时间已更新");
            }else {
                //根据日志id更新签退时间
                signService.updateBySignId(empSign);
                return Result.ok("晚上好！");
            }
        }

    }

}
